﻿package com
{
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.*;
	
	import fl.transitions.*;
	import fl.transitions.easing.*;
	
	public class Image extends MovieClip
	{
		//private var _preloader:MovieClip;
		
		public function Image(imgObj:Object) 
		{
			loadImage(imgObj)
		}
	
		public function update(e:ProgressEvent):void 
		{
			var percent:Number = Math.floor( (e.bytesLoaded*100)/e.bytesTotal );
			if (preloader is MovieClip) 
			{
				preloader.gotoAndStop(percent);
				load_txt.text = percent+"%";
			}
			if (percent == 100) 
			{
				dispatchEvent(new Event("ImageLoaded", true));
				preloader.alpha = 0;
				removeChild(load_txt);
				removeChild(loaderTxt);
			}
		}
		
		private function loadImage(imgObj:Object):void
		{
			var loader:Loader = new Loader();
			loader.load(new URLRequest(imgObj.path));
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, update);
			addChild(loader);
		}
		
		public function fade(IN:Boolean):void
		{
			//TransitionManager.start(this as MovieClip, {type:Fade, direction:Transition.OUT, duration:9, easing:Strong.easeOut});
			if(IN)
			{
				this.addEventListener(Event.ENTER_FRAME, fadeIn);
			}
			else
			{
				this.addEventListener(Event.ENTER_FRAME, fadeOut);
			}
		}
		
		private function fadeIn(evt:Event):void
		{
			
			if(this.alpha<1)
			{
				this.alpha += .05;
			}
			else
			{
				this.removeEventListener(Event.ENTER_FRAME,fadeIn);
				this.alpha = 1;
			}
		}
		
		private function fadeOut(evt:Event):void
		{
			trace(this.alpha)
			if(this.alpha > 0)
			{
				this.alpha -= .05;
			}
			else
			{
				this.removeEventListener(Event.ENTER_FRAME,fadeOut);
				this.alpha = 0;
			}
		}
	}
}
	

